#include "linkLoopList.h"
void initLinkLoopList(LinkLoopList *linkLoopList) {
    linkLoopList->num = 0;
    linkLoopList->rear = linkLoopList->header.next = &linkLoopList->header;
}

int insertLinkLoopHeader(LinkLoopList *linkLoopList, Element value) {
    LoopNode *node = (LoopNode *) malloc(sizeof(LoopNode));
    node->val = value;
    node->next = linkLoopList->header.next;
    linkLoopList->header.next = node;
    if (linkLoopList->rear == &linkLoopList->header) {
        linkLoopList->rear = node;
    }
    ++linkLoopList->num;
    return 0;
}

// 单向循环链表的头节点是没有有效数据，要从下一个元素开始，头目的只是为了边界
void showLinkLoopList(LinkLoopList *linkLoopList) {
    LoopNode *node = linkLoopList->header.next;
    while (node != &linkLoopList->header) {
        printf("%d\t", node->val);
        node = node->next;
    }
    printf("\n");
}

void releaseLoopList(LinkLoopList *linkLoopList) {
    LoopNode *node = linkLoopList->header.next;
    LoopNode *tmp;
    while (node != &linkLoopList->header) {
        tmp = node;
        node = node->next;
        free(tmp);
        --linkLoopList->num;
    }
    linkLoopList->header.next = &linkLoopList->header;
    linkLoopList->rear = &linkLoopList->header;
}

int insertLinkLoopRear(LinkLoopList *linkLoopList, Element value) {
    LoopNode *node = (LoopNode *) malloc(sizeof(LoopNode));
    node->val = value;
    node->next = linkLoopList->rear->next;
    if (linkLoopList->rear == &linkLoopList->header) {		// 链表里没有数据
        linkLoopList->rear = node;
        linkLoopList->header.next = node;
    } else {
        linkLoopList->rear->next = node;
        linkLoopList->rear = node;
    }
    ++linkLoopList->num;
    return 0;
}